﻿using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Admin.业务界面
{
    public partial class config : Form
    {
        List<Sys_config> list_Added = new List<Sys_config>();
        BindingList<Sys_config> bconfigs;
        List<Sys_config> configs;
        DateTimePicker dtp = new DateTimePicker();
        public config()
        {
            InitializeComponent();
            dataGridView1.Controls.Add(dtp);
            dtp.Visible = false;
            dtp.Format = DateTimePickerFormat.Custom;
            dtp.CustomFormat = "yyyy-MM-dd HH:mm:ss";
            dtp.TextChanged += new EventHandler(dtp_TextChange);
        }
        private void dtp_TextChange(object sender, EventArgs e)
        {
            dataGridView1.CurrentCell.Value = dtp.Text.ToString();   //时间控件选择时间时，就把时间赋给所在的单元格
            dtp.Visible = false;
        }
        private void config_Load(object sender, EventArgs e)
        {
            refresh();
        }
        private void refresh()
        {
            BLL.B_SysConfig b_config = new BLL.B_SysConfig();
            configs = b_config.ListConfig();
            bconfigs = new BindingList<Sys_config>(configs);
            dataGridView1.DataSource = bconfigs;
        }

        private void Add_Click(object sender, EventArgs e) //添加
        {
            BLL.B_SysConfig b_config = new BLL.B_SysConfig();
            try
            {
                if (b_config.AddConfig(list_Added))
                    MessageBox.Show("添加成功！");
            }
            catch (Tools.Add_Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void Del_Click(object sender, EventArgs e) //删除
        {
            List<Sys_config> conf_deleted = new List<Sys_config>();
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                conf_deleted.Add(row.DataBoundItem as Sys_config);
            }

            BLL.B_SysConfig b_config = new BLL.B_SysConfig();

            try
            {
                if (b_config.Del(conf_deleted))
                    MessageBox.Show("删除成功！");

                refresh();
            }
            catch (Tools.Del_Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void Save_Click(object sender, EventArgs e) //保存
        {
            BLL.B_SysConfig b_config = new BLL.B_SysConfig();
            try
            {
                if (b_config.SaveList(configs))
                    MessageBox.Show("保存成功!");
            }
            catch (Tools.Modify_Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0 || e.RowIndex >= dataGridView1.RowCount || e.ColumnIndex >= dataGridView1.ColumnCount)
                return;

            if (!dataGridView1.Columns[e.ColumnIndex].Name.EndsWith("deadline"))
                return;

            Rectangle _Rectangle = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
            dtp.Size = new Size(_Rectangle.Width, _Rectangle.Height);
            dtp.Location = new Point(_Rectangle.X, _Rectangle.Y);
            dtp.Visible = true;
        }

        private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            list_Added.Add(dataGridView1.CurrentRow.DataBoundItem as Sys_config);
        }

        private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
        {
            dtp.Visible = false;
        }
    }
}
